﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Data.Entity;
using System.Web;
using EMRI.EmergencyCare.Web.DataContext;
using EMRI.EmergencyCare.Web.Models;

namespace EMRI.EmergencyCare.Web.Helper
{
    public class PatientHelper
    {
        public static int GetNextPatientID()
        {
            return 0;
        }

        public static PatientDetails GetNewPatientDetails(int patientId)
        {
            return new PatientDetails()
                {
                    PatientId = patientId,
                    PatientInDateTime = DateTime.Now,
                    InitialAssesment = new InitialAssesment()
                        {
                            PatientId = patientId
                        },
                    DetailedAssesment = new DetailedAssesment()
                        {
                            PatientId = patientId,
                            Readings = new List<Reading>()
                                {
                                    new Reading() {PatientId = patientId},
                                    new Reading() {PatientId = patientId},
                                    new Reading() {PatientId = patientId},
                                    new Reading() {PatientId = patientId},
                                    new Reading() {PatientId = patientId},
                                    new Reading() {PatientId = patientId},
                                    new Reading() {PatientId = patientId}
                                }
                        },
                    Investigation = new Investigation()
                        {
                            PatientId = patientId
                        },
                    Treatment = new Treatment()
                        {
                            PatientId = patientId,
                            LifeLines = new List<LifeLine>()
                                {
                                    new LifeLine() {PatientId = patientId},
                                    new LifeLine() {PatientId = patientId}
                                },
                            TreatmentDetails = new List<TreatmentDetail>()
                                {
                                    new TreatmentDetail() {PatientId = patientId},
                                    new TreatmentDetail() {PatientId = patientId},
                                    new TreatmentDetail() {PatientId = patientId},
                                    new TreatmentDetail() {PatientId = patientId},
                                    new TreatmentDetail() {PatientId = patientId},
                                    new TreatmentDetail() {PatientId = patientId},
                                    new TreatmentDetail() {PatientId = patientId}
                                }
                        },
                    Synopsis = new Synopsis() { PatientId = patientId, DischargeDateTime = DateTime.Now },
                };
        }

        public static PatientDetails GetPatientDetailsById(int patientId, EmergencyManagementContext db)
        {
            return db.PatientDetails.Include(d => d.DetailedAssesment.Readings)
                .Single(p => p.PatientId == patientId);
        }

        public static bool IsPatientDetailsAvailable(int patientId, EmergencyManagementContext db)
        {
            return db.PatientDetails.Any(p => p.PatientId == patientId);
        }

        public static byte[] GetImage(HttpContextBase context, string key)
        {
            var img = context.Request.Files[key];
            var bytes = new byte[(img == null) ? 0 : img.InputStream.Length];
            if (img != null) img.InputStream.Read(bytes, 0, bytes.Length);
            return bytes;
        }

    }

    public class PatietViewModel
    {
        public Patient Patient { get; set; }

        public bool IsPatientDetailsAvailable { get; set; }
    }

    public class PatientDetailsViewModel
    {
        public Patient Patient { get; set; }

        public PatientDetails PatientDetails { get; set; }

    }
}